package
{
	import flash.desktop.Clipboard;
	import flash.events.NetStatusEvent;
	import flash.external.ExternalInterface;
	import flash.globalization.StringTools;
	import flash.net.GroupSpecifier;
	import flash.net.NetConnection;
	import flash.net.NetGroup;
	import flash.net.URLStream;
	import flash.net.URLVariables;
	import flash.system.Capabilities;
	import flash.system.System;
	import flash.utils.getTimer;
	
	import models.StratusConnection;
	import models.vo.UserVO;
	
	import mx.utils.StringUtil;
	
	import org.dexter.DexterEvent;
	import org.dexter.sendDexterEvent;
	import org.osmf.utils.URL;
	
	import vo.GUserVO;

	public class Stratus extends StratusConnection
	{
		public function Stratus()
		{
			super();
		}
		[DexterExternal]
		public function getPublishURL():String{
			var s:String = UserVO.self.id+"?fms.multicast.type=3&fms.multicast.groupspec="+encodeURIComponent(groupSpecifier.groupspecWithAuthorizations());
			sendDexterEvent("callJs","jsTrace",s);
			return s;
		}
		override protected function onNetStatus(event:NetStatusEvent):void{
			sendDexterEvent("callJs","jsTrace","stratus:"+event.info.code);
			trace(event.info.code);
			switch(event.info.code){
				case "NetConnection.Connect.Failed":
					sendDexterEvent("callJs","jsTrace",uri);
					break;
				case "NetConnection.Connect.Success":
					onConnect();
					break;
				case "NetConnection.Connect.Closed":
					hasConnected = false;
					ExternalInterface.call("alert","连接已断开，请刷新页面");
					break;
				case "NetGroup.Connect.Success":
					hasConnected = true;
					UserVO.self.groupAddress = netGroup.convertPeerIDToGroupAddress(nearID);
					(UserVO.self as GUserVO).p2pId = nearID;
					sendDexterEvent("stratusConnected");
					break;
				case "NetGroup.SendTo.Notify":
					if(!event.info.fromLocal){
						netGroup.sendToNearest(event.info.message, event.info.message.destination);
						break;
					}
				case "NetGroup.Posting.Notify":
					var array:Array = event.info.message as Array;
					if("trust" in array){
						var answer:Array = ["trustAnswer",array["trust"]];
						answer["t"] = getTimer();
						netGroup.sendToNearest(answer,event.info.from);
						if(messageCache.indexOf(array["trust"]) == -1){
							messageCache.push(array["trust"]);
							array[0] = "$"+array[0];
							DexterEvent.SendEvent.apply(DexterEvent,array);
							if(messageCache.length>10){
								messageCache.shift();
							}
						}
					}else{
						array[0] = "$"+array[0];
						DexterEvent.SendEvent.apply(DexterEvent,array);
					}
					break;
			}
			sendDexterEvent("stratusInfo",event);
		}
	}
}